const app = getApp()
import {
  getUserDeliveryAddressList,
  getDeliveryRange,
  editUserDeliveryAddress,
  removeUserDeliveryAddress
} from "../../common/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    deliveryRange: "",
    select: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.options = options;
    if (options !== undefined && options.select !== undefined) {
      this.setData({
        select: options.select
      });
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },
  onRefresh: function () {
    //导航条加载动画
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000);
  },
  onShow: function () {
    let printer = app.globalData.printer;
    if (printer == undefined) {
      wx.showModal({
        title: "提示",
        content: "请先选择打印机",
        showCancel: false,
        confirmText: "去选择",
        success: res2 => {
          if (res2.confirm) {
            wx.navigateTo({
              url: '../shop/index',
            })
          }
        }
      })
    } else {
      this.setData({
        printer: app.globalData.printer
      })
    }
    this.onLoad(this.options);
    this.getAddressList();
  },
  onUnload: function () {
    var arr = this.data.addressList;
    wx.setStorageSync('addressList', arr);
  },
  getAddressList() {
    let printerId = this.data.printer.id;
    wx.showLoading({
      title: '获取地址中',
      mask: true
    })
    let data = {
      printerId: printerId
    }
    getUserDeliveryAddressList(null).then(res => {
      if (res.code == 200) {
        this.setData({
          addressList: res.data
        });
      }
    })
    getDeliveryRange(data).then(res => {
      if (res.code == 200) {
        this.setData({
          // 地址
          deliveryRange: res.data,
          deliveryAddress: "",
        })
      }
    }).catch(err => {
      console.log("获取配送位置报错", err)
    })
    wx.hideLoading();
  },
  addAddress: function () {
    wx.navigateTo({
      url: '/pages/wode/add_location?deliveryRange=' + this.data.deliveryRange
    });
  },
  /* 删除item */
  delAddress: function (e) {
    var id = e.currentTarget.dataset.id //数组下标
    wx.showModal({
      title: "提示",
      content: "确定要删除该地址吗?",
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            title: '正在删除',
          })
          let data = {
            id: id
          }
          removeUserDeliveryAddress(data).then(res => {
            wx.hideLoading();
            if (res.code === 200) {
              this.getAddressList();
            };
          }).catch(err => {
            wx.hideLoading();
            console.log("删除地址报错", err)
          })
        }
      }
    })
  },
  editAddress: function (e) {
    var item = e.currentTarget.dataset.item //数组下标
    console.log(`/pages/wode/add_location?deliveryRange=${this.data.deliveryRange}&contactPhone=${item.contactPhone}&contactName=${item.contactName}&address=${item.address}&id=${item.id}`)
    wx.navigateTo({
      url: `/pages/wode/add_location?deliveryRange=${this.data.deliveryRange}&contactPhone=${item.contactPhone}&contactName=${item.contactName}&address=${item.address}&id=${item.id}`
    });
  },
  //选择签收点
  selectAddress: function (e) {
    let index = e.currentTarget.dataset.index;
    let address = this.data.addressList[index].address;
    let contactPhone = this.data.addressList[index].contactPhone;
    let contactName = this.data.addressList[index].contactName;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]  //上一个页面
    prevPage.setData({
      ["deliveryAddress"]: address,
      ["deliveryContact"]: contactName,
      ["deliveryPhone"]: contactPhone
    });
    prevPage.getTotalFee()
    wx.navigateBack({
      delta: 1,
    })
  },
})